Enter right-side-assign context for ??= so closures capturing the assigned variable by reference see its assigned type#5900
Merged
Merged
Conversation
Contributor
Author
|
extracted from #5895 in which I did a mistake and mixed in a second unrelated bug |
…ssigned variable by reference see its assigned type
- `AssignOpHandler` now calls `ExpressionContext::enterRightSideAssign()` when
evaluating the right-hand side of a `??=` whose target is a simple variable,
mirroring what `AssignHandler` already does for plain `=`/`=&`.
- This lets the by-reference closure-use handling in `NodeScopeResolver`
(which inspects `inAssignRightSideVariableName`/`inAssignRightSideExpr`)
recognise patterns like `$cb ??= function () use (&$cb) { ... }` and resolve
`$cb` to the assigned closure type inside the closure body instead of `null`.
- Without this, `??=` (unlike `=`) left the right-side-assign context empty, so a
recursive `use (&$var)` closure saw `$var` as `null` and produced false
`callable.nonCallable` errors.
- Probed the sibling axis (other assignment operators): plain `=`/`=&` already
set this context; the arithmetic/string compound ops (`+=`, `.=`, ...) cannot
meaningfully capture-and-reassign the same variable by reference, so only
`??=` needed the change.
VincentLanglet
approved these changes
Jun 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a closure captured a variable by reference and that same variable was the target of a
??=assignment, e.g.PHPStan inferred
$isSupportedasnullinside the closure body and reported a falseTrying to invoke null but it's not a callable.error. The exact same pattern written with a plain assignment (if (!isset($isSupported)) { $isSupported = function () use (&$isSupported) ... }) worked correctly. This change makes??=behave like=for this case.Changes
src/Analyser/ExprHandler/AssignOpHandler.php: when evaluating the right-hand side of anExpr\AssignOp\Coalesce(??=) whose target is a simple variable, the handler now callsExpressionContext::enterRightSideAssign($var->name, $expr->expr), the same callAssignHandleralready makes for=/=&.tests/PHPStan/Analyser/nsrt/bug-13810.php: regression test asserting the closure type is inferred for a??=recursive by-reference closure capture.Root cause
The by-reference closure-use handling in
NodeScopeResolver::processClosureExpr()readsinAssignRightSideVariableName/inAssignRightSideExprfrom the currentExpressionContext. When the captured variable matches the variable currently being assigned, it resolves the variable to the assigned (closure) type inside the closure body, which is what makes$x = function () use (&$x) { ... }resolve$xto the closure.Only
AssignHandler(plain=/=&) populated that context.AssignOpHandler, which handles??=together with the arithmetic/string compound operators, never calledenterRightSideAssign(), so for??=the context stayed empty and the by-reference variable fell back to its pre-assignment type (nullafter the??=falsey filter). The fix populates the context for??=, the only compound operator for which capturing-and-reassigning the same variable by reference is meaningful.I probed the sibling assignment operators on the same axis: plain
=/=&already set the context; the arithmetic and string compound ops (+=,-=,.=, ...) require the left-hand variable to already hold a non-closure value and cannot meaningfully reassign it to a captured closure, so they did not need the change.Test
tests/PHPStan/Analyser/nsrt/bug-13810.phpasserts that inside$isSupported ??= function (mixed $arg) use (&$isSupported) { ... }the variable$isSupportedisClosure(mixed): bool(previouslynull), andmixed~nullin the enclosing scope after the??=. Verified to fail before the fix (Actual: null) and pass after.Fixes phpstan/phpstan#13810